package org.nmgyjt.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nimbusds.jose.JOSEException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nmgyjt.springboot.beans.admin.*;
import org.nmgyjt.springboot.entity.admin.*;
import org.nmgyjt.springboot.entity.area.Area;
import org.nmgyjt.springboot.entity.exception.BizException;
import org.nmgyjt.springboot.entity.expert.ExpertInfo;
import org.nmgyjt.springboot.entity.sms.YuFuMSSendEntity;
import org.nmgyjt.springboot.mapper.CsSystemUserMapper;
import org.nmgyjt.springboot.service.*;
import org.nmgyjt.springboot.utils.HttpUtils;
import org.nmgyjt.springboot.utils.IDAASUserUtils;
import org.nmgyjt.springboot.utils.NMGYJTIDAASTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-07-03
 */
@Service
@Slf4j
public class CsSystemUserServiceImpl extends ServiceImpl<CsSystemUserMapper, CsSystemUser> implements ICsSystemUserService {

    @Autowired
    ICsSystemAdminService adminService;

    @Autowired
    ICsSystemAdminMenuRelService menuRelService;

    @Autowired
    ICsSystemDeptService deptService;

    @Autowired
    IIdaasAreaService idaasAreaService;

    @Autowired
    IAreaService areaService;

    @Autowired
    CsSystemDeptServiceImpl deptServiceImpl;

    @Autowired
    ICsSystemSourceExtService csSystemSourceExtService;

    @Autowired
    ICsSystemSourceTypeService iCsSystemSourceTypeService;

    @Autowired
    ExpertService expertService;

    @Autowired
    SendSMSServiceImpl smsService;


    @Override
    //@CacheEvict(cacheNames = "selectUserInfoFromIDAASByOpenIdV2",allEntries = true)
    public UserInfo createCsSystemUser(CsSystemUserCreateBean bean) throws ParseException, JOSEException {

        UserInfo loginAdminInfo = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getCreatedBy());
        if (ObjectUtils.isEmpty(loginAdminInfo)) {
            log.error("创建统一认证用户的管理员账号非法,创建失败,创建信息如下:{}", bean);
            throw new BizException("创建统一认证用户失败,请联系系统管理员!");
        }
        UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
        if (null != info && StringUtils.isNotEmpty(info.getId())) {
            if ("INACTIVE".equals(info.getValues().getStatus())) {
                HashMap<String, String> headerParams = new HashMap<>();
                headerParams.put("Content-Type", "application/json");
                headerParams.put("Authorization", "bearer " + NMGYJTIDAASTokenUtils.getToken());
                String userPostRes2 =
                        HttpUtils.sendHttpPost("http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/" + info.getId() + "/enable", new JSONObject(), headerParams);
            }
            bean.setId(info.getId());
            return updateCsSystemUser(bean, loginAdminInfo);
        }

        CsSystemAdmin admin = adminService.getOne(new LambdaQueryWrapper<CsSystemAdmin>().eq(CsSystemAdmin::getPhone, bean.getCreatedBy()).eq(CsSystemAdmin::getOrgId, bean.getOrgID()).orderByDesc(CsSystemAdmin::getId).last("limit 1"));
        bean.setAreaCode(StringUtils.rightPad(loginAdminInfo.getValues().getRegionCode(), 12, "0"));

        if ("1000000017".equals(bean.getMenuID())
                || "1000000009".equals(bean.getMenuID())
                || "1000000001".equals(bean.getMenuID())
                || "1000000002".equals(bean.getMenuID())) {
            bean.setOrgName(admin.getOrgName());
        }

        CsSystemAdminMenuRel menuRel = menuRelService.getById(bean.getMenuID());
        if ("1000000017".equals(bean.getMenuID())) {
            bean.setMenuName("工委管理员");
        } else {
            bean.setMenuName(menuRel.getName());
        }

        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        headerParams.put("Authorization", "bearer " + NMGYJTIDAASTokenUtils.getToken());

        Map<String, String> bodyParams = new HashMap();
        bodyParams.put("object_type", "DEPT");
        IDAASDeptResponse interDeptResponse = new IDAASDeptResponse();
        String filterOrgName = null;
        String orgId = null;

        if (!"指挥体系成员".equals(bean.getMenuName())) {
            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + loginAdminInfo.getValues().getDeptId() + "/children";
            String str1 = HttpUtils.httpGet(url1, bodyParams, headerParams);
            interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, bean.getOrgName());
        }

        //要创建用户的机构:
        //自治区党委,自治区政府
        //盟市党委（盟委），盟市政府（行署）
        //旗县党委,旗县政府

        if (("党委管理员".equals(bean.getMenuName()) || "政府管理员".equals(bean.getMenuName()))) {

            Area area = areaService.getById(StringUtils.rightPad(loginAdminInfo.getValues().getRegionCode(), 12, "0"));
            if ("Province".equals(area.getRemark())) {

                filterOrgName = "党委管理员".equals(bean.getMenuName()) ? "自治区党委" : "自治区政府";

            } else if ("City".equals(area.getRemark())) {

                filterOrgName = "党委管理员".equals(bean.getMenuName()) ? "盟市党委（盟委）" : "盟市政府（行署）";

            } else if ("County".equals(area.getRemark())) {

                filterOrgName = "党委管理员".equals(bean.getMenuName()) ? "旗县党委" : "旗县政府";

            }

            bean.setOrgName(filterOrgName);

            String userOrgId = null;
            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
            String str1 = HttpUtils.httpGet(url1, bodyParams, headerParams);
            if (0 == bean.getIsLeader()) {
                String userOrgName = "党委管理员".equals(bean.getMenuName()) ? "党委总值班室" : "政府总值班室";
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, userOrgName);

                if (null == interDeptResponse || StringUtils.isEmpty(interDeptResponse.getId())) {
                    CsSystemOrgCreateBean createBean = new CsSystemOrgCreateBean();
                    createBean.setOrgName(userOrgName);
                    createBean.setAreaCode(bean.getAreaCode());
                    IDAASOrgEntity orgToIDAAS = deptServiceImpl.createOrgToIDAAS(createBean, userOrgName, bean.getOrgID());
                    userOrgId = orgToIDAAS.getId();
                } else {
                    userOrgId = interDeptResponse.getId();
                }
            } else {
                String leaderOrgName = "党委管理员".equals(bean.getMenuName()) ? "党委领导" : "政府领导";
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, leaderOrgName);

                /**
                 *   过滤不到相关的党委领导/政府领导节点,则以当前管理员管理的节点为主节点挂靠人员
                 */
                if (null == interDeptResponse || StringUtils.isEmpty(interDeptResponse.getId())) {
                    CsSystemOrgCreateBean createBean = new CsSystemOrgCreateBean();
                    createBean.setOrgName(leaderOrgName);
                    createBean.setAreaCode(bean.getAreaCode());
                    IDAASOrgEntity orgToIDAAS = deptServiceImpl.createOrgToIDAAS(createBean, leaderOrgName, bean.getOrgID());
                    userOrgId = orgToIDAAS.getId();
                } else {
                    userOrgId = interDeptResponse.getId();
                }
            }

            return createUserToIDAAS(bean, userOrgId, loginAdminInfo.getValues().getRegionCode());
        } else if ("工委管理员".equals(bean.getMenuName())) {

            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
            String str1 = HttpUtils.httpGet(url1, bodyParams, headerParams);
            if (0 == bean.getIsLeader()) {
                String userOrgName = "工委值班室";
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, userOrgName);
                if (null == interDeptResponse || StringUtils.isEmpty(interDeptResponse.getId())) {

                    CsSystemOrgCreateBean createBean = new CsSystemOrgCreateBean();
                    createBean.setOrgName(userOrgName);
                    createBean.setAreaCode(bean.getAreaCode());
                    IDAASOrgEntity orgToIDAAS = deptServiceImpl.createOrgToIDAAS(createBean, userOrgName, admin.getOrgId());
                    return createUserToIDAAS(bean, orgToIDAAS.getId(), loginAdminInfo.getValues().getRegionCode());
                }
            } else {
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, "工委领导");

                /**
                 *   过滤不到相关的党委领导/政府领导节点,则以当前管理员管理的节点为主节点挂靠人员
                 */
                if (StringUtils.isEmpty(interDeptResponse.getId()))
                    return createUserToIDAAS(bean, admin.getOrgId(), loginAdminInfo.getValues().getRegionCode());
            }

            return createUserToIDAAS(bean, interDeptResponse.getId(), loginAdminInfo.getValues().getRegionCode());
        } else if ("单位领导".equals(menuRel.getName())) {

            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
            String str1 = HttpUtils.httpGet(url1, bodyParams, headerParams);
            interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, "单位领导");
            if (StringUtils.isNotEmpty(interDeptResponse.getId())) {
                return createUserToIDAAS(bean, interDeptResponse.getId(), loginAdminInfo.getValues().getRegionCode());
            } else {
                /**
                 * 否则:
                 * 1.先创建"单位领导"节点
                 * 2.后添加人员
                 */
                IDAASOrgEntity idaasOrg = createOrgToIDAAS(bean, "单位领导", admin.getOrgId());
                return createUserToIDAAS(bean, idaasOrg.getId(), loginAdminInfo.getValues().getRegionCode());
            }
        } else if ("内设机构".equals(bean.getMenuName())) {
            //获取子部门相关信息:/api/v1/departments/{department_id}/children
            String url2 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + loginAdminInfo.getValues().getDeptId() + "/traces";
            String str2 = HttpUtils.httpGet(url2, bodyParams, headerParams);
            interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str2, "内设机构");
            if (!ObjectUtils.isEmpty(interDeptResponse.getId())) {
                /**
                 * 返回的子部门有内设机构就不用创建,直接获取相关内设机构信息,创建部门
                 */

                String url3 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + interDeptResponse.getId() + "/children";
                String str3 = HttpUtils.httpGet(url3, bodyParams, headerParams);
                IDAASDeptResponse deptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str3, admin.getOffice());
                if (StringUtils.isEmpty(deptResponse.getId())) {
                    IDAASOrgEntity idaas = createOrgToIDAAS(bean, "", interDeptResponse.getId());
                    if ( idaas.getId() == null){
                        return  createUserToIDAAS(bean, loginAdminInfo.getValues().getDeptId(), loginAdminInfo.getValues().getRegionCode());
                    }else {
                        return createUserToIDAAS(bean, idaas.getId(), loginAdminInfo.getValues().getRegionCode());
                    }

                } else {
                    return createUserToIDAAS(bean, deptResponse.getId(), loginAdminInfo.getValues().getRegionCode());
                }
            }
        } else {
            orgId = admin.getOrgId();
            if ("1000000013".equals(bean.getMenuID())) {
                /**
                 *  专家管理员二次修改后挂在了专家节点,所以要获取管理员管理的节点信息
                 */
                orgId = admin.getOrgId();
            }
            UserInfo user = createUserToIDAAS(bean, orgId, loginAdminInfo.getValues().getRegionCode());
            if (null != user && "1000000013".equals(bean.getMenuID())) {
                CsSystemSourceExt sourceExt = new CsSystemSourceExt();
                sourceExt.setSourceType(bean.getSourceType());
                sourceExt.setRemark(bean.getDesc());
                sourceExt.setOrgId(user.getId());
                sourceExt.setSuperiorOrgName(admin.getOrgName());
                sourceExt.setSuperiorOrgId(admin.getOrgId());
                sourceExt.setMenuId(bean.getMenuID());
                csSystemSourceExtService.save(sourceExt);

                try {
                    ExpertInfo expertInfo = new ExpertInfo();
                    expertInfo.setExpertName(bean.getName());
//                    CsSystemSourceType csSystemSourceType = iCsSystemSourceTypeService.getById(sourceExt.getSourceType());
//                    expertInfo.setExpertType(csSystemSourceType.getName());
//                    expertInfo.setExpertTypeCode(csSystemSourceType.getId());
                    JSONArray jsonArray = JSON.parseArray(sourceExt.getSourceType());
                    log.info("*********获取类型数组：{}", jsonArray);
                    if (jsonArray.size() > 0) {
                        String sourceType = jsonArray.getString(jsonArray.size() - 1);
                        if (StringUtils.isEmpty(sourceType)) {
                            sourceType = jsonArray.getString(jsonArray.size() - 2);
                            CsSystemSourceType csSystemSourceType = iCsSystemSourceTypeService.getById(sourceType);
                            expertInfo.setExpertType(csSystemSourceType.getName());
                            expertInfo.setExpertTypeCode(csSystemSourceType.getId());
                        } else {
                            CsSystemSourceType csSystemSourceType = iCsSystemSourceTypeService.getById(sourceType);
                            log.info("*********获取类型对象：{}", csSystemSourceType.toString());
                            expertInfo.setExpertType(csSystemSourceType.getName());
                            expertInfo.setExpertTypeCode(csSystemSourceType.getId());
                        }

                    }

                    if (bean.getAreaCode() !=null ){
                        expertInfo.setAreaCode(bean.getAreaCode().substring(0, 6));
                    }
                    expertInfo.setDepId(user.getValues().getDeptId());
                    expertInfo.setPhone(bean.getPhone());
                    expertInfo.setDepartment(user.getValues().getWorkUnit());
                    expertInfo.setAddress(bean.getOfficeAddress());
                    expertInfo.setJob(bean.getJob());
                    JSONArray jobLevel = JSON.parseArray(bean.getJobLevel());
                    expertInfo.setJobLevelName(jobLevel.getString(0));
                    expertInfo.setJobLevel(bean.getJobLevel());
//                    expertInfo.setJobLevelName(bean.getJobLevel());
//                    expertInfo.setJobLevel(bean.getJobLevel());
                    JSONArray userLevel = JSON.parseArray(bean.getUserLevel());
                    expertInfo.setLeveledName(userLevel.getString(0));
                    expertInfo.setLeveled(bean.getUserLevel());
                    if ("[\"乡镇级\"]".equals(bean.getUserLevel())) {
                        expertInfo.setSyncLevel("5");
                    }
                    if ("[\"旗县级\"]".equals(bean.getUserLevel())) {
                        expertInfo.setSyncLevel("4");
                    }
                    if ("[\"盟市级\"]".equals(bean.getUserLevel())) {
                        expertInfo.setSyncLevel("3");
                    }
                    if ("[\"省/自治区级\"]".equals(bean.getUserLevel())) {
                        expertInfo.setSyncLevel("2");
                    }
                    if ("[\"国家级\"]".equals(bean.getUserLevel())) {
                        expertInfo.setSyncLevel("1");
                    }
//                    expertInfo.setLeveledName(bean.getUserLevel());
//                    expertInfo.setLeveled(bean.getUserLevel());
//                    if ("乡镇级".equals(bean.getUserLevel())) {
//                        expertInfo.setSyncLevel("5");
//                    }
//                    if ("旗县级".equals(bean.getUserLevel())) {
//                        expertInfo.setSyncLevel("4");
//                    }
//                    if ("盟市级".equals(bean.getUserLevel())) {
//                        expertInfo.setSyncLevel("3");
//                    }
//                    if ("省/自治区级".equals(bean.getUserLevel())) {
//                        expertInfo.setSyncLevel("2");
//                    }
//                    if ("国家级".equals(bean.getUserLevel())) {
//                        expertInfo.setSyncLevel("1");
//                    }

                    expertInfo.setDsc(bean.getDesc());
                    expertInfo.setUserId(user.getId());
                    expertInfo.setSuperiorOrgName(admin.getOrgName());
                    expertInfo.setSuperiorOrgId(admin.getOrgId());
                    expertService.saveOrUpdate(expertInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            updateOrgUserNumber(bean.getOrgID());
            return user;
        }
        return null;
    }

    @Override
    public List<CsSystemUserQueryBean> selectCsSystemUser(CsSystemUserSelectBean bean) throws ParseException, JOSEException {

       // UserInfo loginAdminInfo = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getCreatedBy());
        LambdaQueryWrapper<CsSystemAdmin> queryWrapper = new LambdaQueryWrapper<CsSystemAdmin>().eq(CsSystemAdmin::getPhone, bean.getCreatedBy());
        if (!"1000000001".equals(bean.getMenuID()) && !"1000000002".equals(bean.getMenuID()) && !"1000000017".equals(bean.getMenuID())) {
            queryWrapper.eq(CsSystemAdmin::getOrgId, bean.getOrgId());
        } else {
            queryWrapper.eq(CsSystemAdmin::getSuperiorOrg, bean.getOrgId());
            queryWrapper.eq(CsSystemAdmin::getMenuId, bean.getMenuID());
        }
       /* else if ("1000000001".equals(bean.getMenuID()) || "1000000002".equals(bean.getMenuID())) {

        }*/

        CsSystemAdmin admin = adminService.getOne(queryWrapper);

        //解决非详情当前节点管理员登录的错误问题
        if (null == admin && ("1000000001".equals(bean.getMenuID()) || "1000000002".equals(bean.getMenuID()) || "1000000017".equals(bean.getMenuID()))) {
            queryWrapper = new LambdaQueryWrapper<CsSystemAdmin>().eq(CsSystemAdmin::getPhone, bean.getCreatedBy());
            queryWrapper.eq(CsSystemAdmin::getOrgId, bean.getOrgId());
            admin = adminService.getOne(queryWrapper);
        }

        CsSystemAdminMenuRel menuRel = menuRelService.getById(bean.getMenuID());
        if ("1000000017".equals(bean.getMenuID())) {
            bean.setMenuName("工委管理员");
        } else {
            bean.setMenuName(menuRel.getName());
        }
        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        headerParams.put("Authorization", "bearer " + NMGYJTIDAASTokenUtils.getToken());

        String orgId = null;
        List<String> orgIdList = new ArrayList<>();
        Map<String, String> bodyParam = new HashMap();
        bodyParam.put("object_type", "DEPT");
        if ("党委管理员".equals(bean.getMenuName()) || "政府管理员".equals(bean.getMenuName())) {
            bean.setOrgName("管理员");


            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
            String str1 = HttpUtils.httpGet(url1, bodyParam, headerParams);
            IDAASDeptResponse interDeptResponse = null;

            if (0 == bean.getIsLeader()) {
                String filterOrgName2 = "党委管理员".equals(bean.getMenuName()) ? "党委总值班室" : "政府总值班室";
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, filterOrgName2);
            } else {
                String filterOrgName = "党委管理员".equals(bean.getMenuName()) ? "党委领导" : "政府领导";
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, filterOrgName);
            }


            if (null != interDeptResponse && StringUtils.isNotEmpty(interDeptResponse.getId())) {
                orgId = interDeptResponse.getId();
                orgIdList.add(interDeptResponse.getId());
            } /*else {
                orgId = admin.getOrgId();
                orgIdList.add(admin.getOrgId());
            }*/

        } else if ("工委管理员".equals(bean.getMenuName())) {
            bean.setOrgName("管理员");

            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
            String str1 = HttpUtils.httpGet(url1, bodyParam, headerParams);
            IDAASDeptResponse interDeptResponse = null;

            if (0 == bean.getIsLeader()) {
                interDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, "工委值班室");
            }


            if (null != interDeptResponse && StringUtils.isNotEmpty(interDeptResponse.getId())) {
                orgId = interDeptResponse.getId();
                orgIdList.add(interDeptResponse.getId());
            } else {
                orgId = admin.getOrgId();
                orgIdList.add(admin.getOrgId());
            }

        } else if ("单位领导".equals(bean.getMenuName())) {
            String url2 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
            String str2 = HttpUtils.httpGet(url2, bodyParam, headerParams);
            IDAASDeptResponse deptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str2, "单位领导");
            orgId = deptResponse.getId();
            orgIdList.add(orgId);
        } else if ("内设机构".equals(bean.getMenuName())) {
            orgId = admin.getOrgId();
            orgIdList.add(orgId);
        } else if ("专家库".equals(bean.getMenuName())) {
            orgId = admin.getOrgId();
            orgIdList.add(orgId);
        } else {
            orgId = admin.getOrgId();
            orgIdList.add(orgId);
        }


        Map bodyParam2 = new HashMap();
        bodyParam2.put("status", new ArrayList<>());

        bodyParam2.put("deptId", orgIdList);
        bodyParam2.put("identityClassification", "defaultIdentityClassification");
        Map bodyParams = new HashMap();
        bodyParams.put("objectType", "USER");
        bodyParams.put("values", bodyParam2);

        List<CsSystemUserQueryBean> users = new ArrayList<>();
        if (StringUtils.isNotEmpty(orgId)) {
            String url2 = "http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/top_user/" + orgId + "/query?page_index=0&page_size=200&order_by=_udOrder&order_by=_displayName";
            String str2 = HttpUtils.sendHttpPost(url2, (JSONObject) JSON.toJSON(bodyParams), headerParams);
            IDAASUserResponse object = JSONObject.parseObject(str2, IDAASUserResponse.class);

            if (null != object && !CollectionUtils.isEmpty(object.getData())) {
                int i = 1;
                for (UserInfo user : object.getData()) {
                    if (bean.getIsLeader() == user.getValues().getIsLeader()) {
                        CsSystemUserQueryBean userBean = new CsSystemUserQueryBean();

                        userBean.setSuperiorOrg(admin.getSuperiorOrg());
                        userBean.setAreaName(admin.getAreaName());
                        userBean.setAreaCode(admin.getAreaCode());
                        userBean.setOrgID(admin.getOrgId());
                        userBean.setOrgName(admin.getOffice());

                        userBean.setIsLeader(user.getValues().getIsLeader());
                        userBean.setJob(user.getValues().getJobPosition());
                        userBean.setName(user.getValues().getDisplayName());
                        userBean.setPhone(user.getValues().getUsername());
//                        userBean.setOrgName(user.getValues().getWorkUnit());
                        userBean.setSuperiorOrgName(user.getValues().getWorkUnit());
                        userBean.setUdOrder(user.getValues().getUdOrder());
                        userBean.setWorkUnit(user.getValues().getWorkUnit());
                        userBean.setOfficeAddress(user.getValues().getOfficeAddress());
                        userBean.setId(user.getId());
                        if ("1000000016".equals(bean.getMenuID()) && StringUtils.isEmpty(userBean.getWorkUnit())) {
                            userBean.setWorkUnit(user.getValues().getOfficeAddress());
                        }
                        if (null == userBean.getUdOrder()) {
                            userBean.setUdOrder(i);
                        } else {
                            i = userBean.getUdOrder();
                        }

                        if ("1000000013".equals(bean.getMenuID())) {
                            CsSystemSourceExt sourceExt = csSystemSourceExtService.getOne(new LambdaQueryWrapper<CsSystemSourceExt>().eq(CsSystemSourceExt::getMenuId, bean.getMenuID()).eq(CsSystemSourceExt::getOrgId, user.getId()));
                            if (null != sourceExt) {
                                if (null != sourceExt.getSourceType() && sourceExt.getSourceType().contains("[") && sourceExt.getSourceType().contains("]")) {
                                    userBean.setSourceType(JSONObject.parseObject(sourceExt.getSourceType(), List.class));
                                } else {
                                    userBean.setSourceType(Arrays.asList(sourceExt.getSourceType()));
                                }

                                if (null != user.getValues().getJobTitle() && user.getValues().getJobTitle().contains("[") && user.getValues().getJobTitle().contains("]")) {
                                    userBean.setUserLevel(JSONObject.parseObject(user.getValues().getJobTitle(), List.class));
                                } else {
                                    userBean.setUserLevel(Arrays.asList(user.getValues().getJobTitle()));
                                }

                                if (null != user.getValues().getJobLevel() && user.getValues().getJobLevel().contains("[") && user.getValues().getJobLevel().contains("]")) {
                                    userBean.setJobLevel(JSONObject.parseObject(user.getValues().getJobLevel(), List.class));
                                } else {
                                    userBean.setJobLevel(Arrays.asList(user.getValues().getJobLevel()));
                                }
                                // userBean.setUserLevel(Arrays.asList(user.getValues().getJobTitle()));
                                userBean.setDesc(sourceExt.getRemark());
                                // userBean.setJobLevel(Arrays.asList(user.getValues().getJobLevel()));
                                userBean.setJobType(user.getValues().getJobType());
                            }
                        }
                        users.add(userBean);
                        i = i + 1;
                    }
                }
            }
        }

        return users;
    }

    @Override
    //@CacheEvict(cacheNames = "deleteCsSystemUser",allEntries = true)
    public String deleteCsSystemUser(CsSystemDeleteBean bean) throws ParseException, JOSEException {

        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        try {
            headerParams.put("Authorization", "bearer " + NMGYJTIDAASTokenUtils.getToken());
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (JOSEException e) {
            e.printStackTrace();
        }
        String httpRet = HttpUtils.sendHttpPost("http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/" + bean.getId() + "/deactivate", new JSONObject(), headerParams);
        ExpertInfo expertInfo = expertService.getByUserId(bean.getId());
        if (!ObjectUtils.isEmpty(expertInfo)) {
            expertInfo.setState("1");
            expertService.saveOrUpdate(expertInfo);
        }
        return httpRet;
    }

    @Override
    //@CacheEvict(cacheNames = "selectUserInfoFromIDAASByOpenIdV2",allEntries = true)
    public UserInfo updateCsSystemUser(CsSystemUserCreateBean bean, UserInfo loginAdminInfo) throws ParseException, JOSEException {

        UserInfo userInfo = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
        if (null == userInfo || StringUtils.isEmpty(bean.getId())) {
            return this.createCsSystemUser(bean);
        } else {

            CsSystemAdmin admin = adminService.getOne(new LambdaQueryWrapper<CsSystemAdmin>().eq(CsSystemAdmin::getPhone, bean.getCreatedBy()).orderByDesc(CsSystemAdmin::getId).last("limit 1"));

            IDAASUserEntity po = new IDAASUserEntity();
            po.setDisplayName(bean.getName());
            po.setJobPosition(bean.getJob());
            // po.setPrimaryMail(bean.getPhone() + "@qq.com");
            po.setOfficeAddress(bean.getOfficeAddress());
            po.setUsername(bean.getPhone());
            po.setIsLeader(bean.getIsLeader());
            po.setDeptId(admin.getOrgId());
            po.setToWX(1);
            po.setId(bean.getId());
            po.setJobLevel(bean.getJobLevel());
            po.setJobTitle(bean.getUserLevel());
            po.setWorkUnit(bean.getWorkUnit());

            HashMap<String, String> headerParams = new HashMap<>();
            headerParams.put("Content-Type", "application/json");
            headerParams.put("Authorization", "Bearer " + NMGYJTIDAASTokenUtils.getToken());

            if ("1000000009".equals(bean.getMenuID())) {
                String url2 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
                Map<String, String> bodyParam = new HashMap();
                bodyParam.put("object_type", "DEPT");
                String str2 = HttpUtils.httpGet(url2, bodyParam, headerParams);
                IDAASDeptResponse deptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str2, "单位领导");
                po.setDeptId(deptResponse.getId());
            } else if ("1000000001".equals(bean.getMenuID()) || "1000000002".equals(bean.getMenuID())) {

                Map<String, String> bodyParams = new HashMap();
                bodyParams.put("object_type", "DEPT");

                String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + admin.getOrgId() + "/children";
                String str1 = HttpUtils.httpGet(url1, bodyParams, headerParams);
                if (0 == bean.getIsLeader()) {
                    String userOrgName = "1000000001".equals(bean.getMenuID()) ? "党委总值班室" : "政府总值班室";
                    IDAASDeptResponse idaasDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, userOrgName);
                    if (null == idaasDeptResponse || StringUtils.isEmpty(idaasDeptResponse.getId())) {

                        CsSystemOrgCreateBean createBean = new CsSystemOrgCreateBean();
                        createBean.setOrgName(userOrgName);
                        createBean.setAreaCode(bean.getAreaCode());
                        IDAASOrgEntity orgToIDAAS = deptServiceImpl.createOrgToIDAAS(createBean, userOrgName, admin.getOrgId());
                        po.setDeptId(orgToIDAAS.getId());
                    } else {
                        po.setDeptId(idaasDeptResponse.getId());
                    }
                } else {
                    String userOrgName = "1000000001".equals(bean.getMenuID()) ? "党委领导" : "政府领导";
                    IDAASDeptResponse idaasDeptResponse = deptServiceImpl.getOrgInfoFromIDAASOrgList(str1, userOrgName);
                    if (null == idaasDeptResponse || StringUtils.isEmpty(idaasDeptResponse.getId())) {

                        CsSystemOrgCreateBean createBean = new CsSystemOrgCreateBean();
                        createBean.setOrgName(userOrgName);
                        createBean.setAreaCode(bean.getAreaCode());
                        IDAASOrgEntity orgToIDAAS = deptServiceImpl.createOrgToIDAAS(createBean, userOrgName, admin.getOrgId());
                        po.setDeptId(orgToIDAAS.getId());
                    } else {
                        po.setDeptId(idaasDeptResponse.getId());
                    }
                }
            }


            Map map = new HashMap<>();
            map.put("values", po);
            String userPostRes = HttpUtils.sendHttpPut("http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/users/" + bean.getId(), (JSONObject) JSON.toJSON(map), headerParams);
            log.info(userPostRes);

            UserInfo user = JSONObject.parseObject(userPostRes, UserInfo.class);
            if (StringUtils.isNotEmpty(userPostRes)) {
                if ("SUSPENDED".equals(user.getValues().getStatus())) {
                    String userPostRes2 = HttpUtils.sendHttpPost("http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/" + bean.getId() + "/enable", new JSONObject(), headerParams);
                }

                String[] menus = {"1000000010", "1000000011", "1000000012", "1000000013", "1000000014"};
                List<String> menuList = Arrays.asList(menus);
                if (menuList.contains(bean.getMenuID())) {
                    String orgId = "";
                    if ("1000000013".equals(bean.getMenuID())) {
                        orgId = user.getId();
                    } else {
                        orgId = po.getDeptId();
                    }
                    CsSystemSourceExt sourceExt = csSystemSourceExtService.getOne(new LambdaQueryWrapper<CsSystemSourceExt>()
                            .eq(CsSystemSourceExt::getOrgId, orgId));
                    if (ObjectUtils.isEmpty(sourceExt)) {
                        sourceExt = new CsSystemSourceExt();
                    }
                    sourceExt.setSourceType(bean.getSourceType());
                    sourceExt.setRemark(bean.getDesc());
                    sourceExt.setSuperiorOrgName(admin.getOrgName());
                    sourceExt.setSuperiorOrgId(admin.getOrgId());
                    sourceExt.setMenuId(bean.getMenuID());
                    if ("1000000013".equals(bean.getMenuID())) {
                        sourceExt.setOrgId(user.getId());
                    } else {
                        sourceExt.setOrgId(po.getDeptId());
                    }
                    CsSystemSourceExt one = csSystemSourceExtService.getOne(new LambdaQueryWrapper<CsSystemSourceExt>().eq(CsSystemSourceExt::getOrgId, sourceExt.getOrgId()));
                    if (null != one) {
                        sourceExt.setId(one.getId());
                    }
                    csSystemSourceExtService.saveOrUpdate(sourceExt);

                    try {
                        QueryWrapper<ExpertInfo> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("user_id", sourceExt.getOrgId());
                        ExpertInfo expertInfo = expertService.getOne(queryWrapper);
                        if (ObjectUtils.isEmpty(expertInfo)) {
                            log.info("《《《《《《《《《《《《《《《《《《《《《《《《专家新增");
                            expertInfo = new ExpertInfo();
                        }
                        log.info("《《《《《《《《《《《《《《《《《《《《《《《《专家修改516");
                        expertInfo.setExpertName(bean.getName());
//                        CsSystemSourceType csSystemSourceType = iCsSystemSourceTypeService.getById(sourceExt.getSourceType());
//                        expertInfo.setExpertType(csSystemSourceType.getName());
//                        expertInfo.setExpertTypeCode(csSystemSourceType.getId());
                        JSONArray jsonArray = JSON.parseArray(sourceExt.getSourceType());
                        log.info("《《《《《《《《《《《《《《《《《《《《《《《《获取类型数组：{}", jsonArray);
                        if (jsonArray.size() > 0) {
                            String sourceType = jsonArray.getString(jsonArray.size() - 1);
                            if (StringUtils.isEmpty(sourceType)) {
                                sourceType = jsonArray.getString(jsonArray.size() - 2);
                                CsSystemSourceType csSystemSourceType = iCsSystemSourceTypeService.getById(sourceType);
                                expertInfo.setExpertType(csSystemSourceType.getName());
                                expertInfo.setExpertTypeCode(csSystemSourceType.getId());
                            } else {
                                CsSystemSourceType csSystemSourceType = iCsSystemSourceTypeService.getById(sourceType);
                                log.info("《《《《《《《《《《《《《《《《《《《《《《《《获取类型对象：{}", csSystemSourceType.toString());
                                expertInfo.setExpertType(csSystemSourceType.getName());
                                expertInfo.setExpertTypeCode(csSystemSourceType.getId());
                            }

                        }
                        if (bean.getAreaCode() !=null ){
                            expertInfo.setAreaCode(bean.getAreaCode().substring(0, 6));
                        }
                        expertInfo.setDepId(admin.getOrgId());
                        expertInfo.setPhone(bean.getPhone());
//                    expertInfo.setDepartment(bean.getOrgName());
                        expertInfo.setDepartment(user.getValues().getWorkUnit());
                        expertInfo.setAddress(bean.getOfficeAddress());
                        expertInfo.setJob(bean.getJob());
                        log.info("《《《《《《《《《《《《《《《《《《《《《《《《专家修改542");
                        JSONArray jobLevel = JSON.parseArray(bean.getJobLevel());
                        expertInfo.setJobLevelName(jobLevel.getString(0));
                        expertInfo.setJobLevel(bean.getJobLevel());
//                        expertInfo.setJobLevelName(bean.getJobLevel());
//                        expertInfo.setJobLevel(bean.getJobLevel());
                        JSONArray userLevel = JSON.parseArray(bean.getUserLevel());
                        expertInfo.setLeveledName(userLevel.getString(0));
                        expertInfo.setLeveled(bean.getUserLevel());
                        if ("[\"乡镇级\"]".equals(bean.getUserLevel())) {
                            expertInfo.setSyncLevel("5");
                        }
                        if ("[\"旗县级\"]".equals(bean.getUserLevel())) {
                            expertInfo.setSyncLevel("4");
                        }
                        if ("[\"盟市级\"]".equals(bean.getUserLevel())) {
                            expertInfo.setSyncLevel("3");
                        }
                        if ("[\"省/自治区级\"]".equals(bean.getUserLevel())) {
                            expertInfo.setSyncLevel("2");
                        }
                        if ("[\"国家级\"]".equals(bean.getUserLevel())) {
                            expertInfo.setSyncLevel("1");
                        }
//                        expertInfo.setLeveledName(bean.getUserLevel());
//                        expertInfo.setLeveled(bean.getUserLevel());
//                        log.info("《《《《《《《《《《《《《《《《《《《《《《《《专家修改549");
//                        if ("乡镇级".equals(bean.getUserLevel())) {
//                            expertInfo.setSyncLevel("5");
//                        }
//                        if ("旗县级".equals(bean.getUserLevel())) {
//                            expertInfo.setSyncLevel("4");
//                        }
//                        if ("盟市级".equals(bean.getUserLevel())) {
//                            expertInfo.setSyncLevel("3");
//                        }
//                        if ("省/自治区级".equals(bean.getUserLevel())) {
//                            expertInfo.setSyncLevel("2");
//                        }
//                        if ("国家级".equals(bean.getUserLevel())) {
//                            expertInfo.setSyncLevel("1");
//                        }
                        expertInfo.setDsc(bean.getDesc());
                        expertInfo.setUserId(user.getId());
                        expertInfo.setSuperiorOrgName(admin.getOrgName());
                        expertInfo.setSuperiorOrgId(admin.getOrgId());
                        Boolean b = expertService.saveOrUpdate(expertInfo);
                        log.info("《《《《《《《《《《《《《《《《《《《《《《《《专家修改568:{}", b);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return user;
        }
    }

    @Override
    public String udOrderCsSystemUser(List<CsSystemUdOrderBean> udOrders) throws ParseException, JOSEException {

        if (!CollectionUtils.isEmpty(udOrders)) {
            HashMap<String, String> headerParams = new HashMap<>();
            headerParams.put("Authorization", "bearer " + NMGYJTIDAASTokenUtils.getToken());
            headerParams.put("Content-Type", "application/json");

            JSONObject bodyParams = new JSONObject();

            // bodyParams.put("object_type", "DEPT");
            udOrders.forEach(ud -> {
                Map<String, Integer> map = new HashMap();
                map.put("udOrder", Integer.valueOf(ud.getUdOrder()));
                String url1 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/users/" + ud.getId();
                bodyParams.put("values", map);
                String str1 = HttpUtils.sendHttpPut(url1, (JSONObject) JSON.toJSON(bodyParams), headerParams);
                log.info(str1);
            });
        }
        return "";
    }


    /**
     * 根据部门查询相关部门;
     */
    IDAASDeptResponse selectDeptFromIDAASByDeptName(String orgName) throws ParseException, JOSEException {
        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        headerParams.put("Authorization", "Bearer " + NMGYJTIDAASTokenUtils.getToken());

        Map<String, String> bodyParams = new HashMap();
        bodyParams.put("object_type", "DEPT");
        bodyParams.put("keywords", orgName);


        String clientPostRes = HttpUtils.httpGet("http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/search");

        //返回来的部门解析获取最终能完全匹配的部门信息;
        IDAASDeptResponse deptResponse = null;

        if (StringUtils.isNotEmpty(clientPostRes) && clientPostRes.contains(orgName)) {
            IDAASDeptListResponse listResponse = JSONObject.parseObject(clientPostRes, IDAASDeptListResponse.class);
            if (!CollectionUtils.isEmpty(listResponse.getData())) {
                for (IDAASDeptResponse dept : listResponse.getData()) {
                    if (orgName.equals(dept.getValues().getName())) {
                        deptResponse = dept;
                        break;
                    }
                }
            }
        }
        return deptResponse;
    }


    /**
     * 创建用户到 IDAAS
     *
     * @param bean
     * @param deptId
     * @param regionCode
     * @return
     * @throws ParseException
     * @throws JOSEException
     */
    UserInfo createUserToIDAAS(CsSystemUserCreateBean bean, String deptId, String regionCode) throws ParseException, JOSEException {
        /**
         *第二步: 查询用户,如果存在就添加用户
         */
        IDAASUserEntity po = new IDAASUserEntity();
        po.setDisplayName(bean.getName());
        po.setJobPosition(bean.getJob());
        po.setPrimaryMail(bean.getPhone() + "@qq.com");
        po.setOfficeAddress(bean.getOfficeAddress());
        if (StringUtils.isEmpty(bean.getOfficeAddress()) && StringUtils.isNotEmpty(bean.getOrgName())) {
            po.setOfficeAddress(bean.getOrgName());
        } else if (StringUtils.isEmpty(bean.getOfficeAddress()) && StringUtils.isNotEmpty(bean.getWorkUnit())) {
            po.setOfficeAddress(bean.getWorkUnit());
        }
        po.setUsername(bean.getPhone());
        po.setDeptId(deptId);
        po.setIsLeader(bean.getIsLeader());
        po.setRegionCode(regionCode);
        po.setToWX(1);
        po.setJobType(bean.getJobType());
        po.setJobLevel(bean.getJobLevel());
        po.setJobTitle(bean.getUserLevel());
        po.setWorkUnit(bean.getWorkUnit());
        if ("1000000015".equals(bean.getMenuID()) && StringUtils.isNotEmpty(bean.getOffice())) {
            po.setWorkUnit(bean.getOffice());
        }
        po.setUdOrder(bean.getUdOrder());

        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        headerParams.put("Authorization", "Bearer " + NMGYJTIDAASTokenUtils.getToken());

        Map map = new HashMap<>();
        map.put("values", po);
        map.put("objectType", "USER");
        String userPostRes = HttpUtils.sendHttpPost("http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects", (JSONObject) JSON.toJSON(map), headerParams);

        if (StringUtils.isNotEmpty(userPostRes)) {
            UserInfo user = JSONObject.parseObject(userPostRes, UserInfo.class);

            // 新增用户以后更新用户的相关机构的人员数量
            if (null != user && StringUtils.isNotEmpty(user.getValues().getDeptId()))
                updateOrgUserNumber(user.getValues().getDeptId());
            Map restBean = new HashMap<>();
            restBean.put("password", "nmgYjt@" + user.getValues().getUsername().substring(7, 11));
            restBean.put("scriptContent", "");
            restBean.put("script", false);
            String url1 = "http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/" + user.getId() + "/reset_password";
            String resetStr = HttpUtils.sendHttpPost(url1, (JSONObject) JSON.toJSON(restBean), headerParams);
            UserInfo userResponse = JSONObject.parseObject(resetStr, UserInfo.class);

            if (null != userResponse && StringUtils.isNotEmpty(userResponse.getValues().getUsername())) {
                //UserInfo userInfo = IDAASUserUtils.selectUserInfoFromIDAASByOpenId(bean.getCreatedBy());
                YuFuMSSendEntity sendEntity = new YuFuMSSendEntity();
                List<String> params = new ArrayList<>();
                List<String> phone = new ArrayList<>();
                phone.add(userResponse.getValues().getUsername());
                params.add(userResponse.getValues().getDisplayName());
                params.add(bean.getName());
                params.add(bean.getPhone());
                params.add(userResponse.getValues().getUsername());
                sendEntity.setType("user.notification");
                sendEntity.setTemplateParamSet(params);
                smsService.sendMS(sendEntity);
            }
            return user;
        }
        return null;
    }


    IDAASOrgEntity createOrgToIDAAS(CsSystemUserCreateBean dept, String orgName, String parentId) throws ParseException, JOSEException {

        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        headerParams.put("Authorization", "Bearer " + NMGYJTIDAASTokenUtils.getToken());

        IDAASOrgEntity po = new IDAASOrgEntity();
        String areaCode = dept.getAreaCode().substring(0, 9);
        po.setOrgType("ob-1def915f1cb04dc190d48ea9cbf0f52c");
        po.setStatus("ENABLED");
        po.setRegionCode(areaCode);
        po.setParentId(parentId);
        po.setArea(idaasAreaService.getOne(new LambdaQueryWrapper<IdaasArea>().eq(IdaasArea::getCode, areaCode)).getId());
        if (StringUtils.isNotEmpty(orgName)) {
            po.setName(orgName);
        } else {
            po.setName(dept.getOrgName());
        }
        Map map = new HashMap<>();
        map.put("values", po);
        String clientPostRes = HttpUtils.sendHttpPost("http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/", (JSONObject) JSON.toJSON(map), headerParams);

        return JSONObject.parseObject(clientPostRes, IDAASOrgEntity.class);
    }

    public static void main(String[] args) {
        Integer l = 10;
        String formattedNumber = String.format("%0" + l + "d", 123);
        System.out.println(formattedNumber); // 输出：000123
    }

    /**
     * 增加一个循环更新机构人数
     *
     * @param orgId
     */
    void updateOrgUserNumber(String orgId) throws ParseException, JOSEException {

        HashMap<String, String> headerParams = new HashMap<>();
        headerParams.put("Content-Type", "application/json");
        headerParams.put("Authorization", "bearer " + NMGYJTIDAASTokenUtils.getToken());

        String url4 = "http://idaas-admin.nmgyjtwg.org.cn/contacts/api/v1/departments/" + orgId;

        Map<String, String> bodyParam = new HashMap();
        bodyParam.put("object_type", "DEPT");

        String clientPostRes = HttpUtils.httpGet(url4, bodyParam, headerParams);
        IDAASDeptResponse orgResponse = JSONObject.parseObject(clientPostRes, IDAASDeptResponse.class);
        List<String> orgIds = orgResponse.getValues().getAncestor();
        orgIds.add(orgId);
        for (String id : orgIds) {

            HashMap bodyParams3 = new HashMap<>();
            Map<Object, Object> objectMap = new HashMap<>();
            objectMap.put("status", new ArrayList<>());
            objectMap.put("identityClassification", "defaultIdentityClassification");
            bodyParams3.put("objectType", "USER");
            List<String> idList = new ArrayList<>();
            idList.add(id);
            objectMap.put("deptId", idList);
            bodyParams3.put("values", objectMap);
            String userNumberUrl = "http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/top_user/" + id + "/query?page_index=0&page_size=10&order_by=_udOrder&order_by=_displayName&need_child_depts=true";
            String str2 = HttpUtils.sendHttpPost(userNumberUrl, (JSONObject) JSON.toJSON(bodyParams3), headerParams);

            IDAASUserResponse object = JSONObject.parseObject(str2, IDAASUserResponse.class);
            Integer userNumber = null == object.getTotal() ? 0 : object.getTotal();

            Map<String, Object> map = new HashMap();
            map.put("userNumber", userNumber);
            Map updateBodyParams = new HashMap<>();

            updateBodyParams.put("values", map);
            String str4 = HttpUtils.sendHttpPut(url4, (JSONObject) JSON.toJSON(updateBodyParams), headerParams);

        }

    }
}